<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 
        await语句后所有的代码都变成异步代码
        await只能等promise实例,其他一概不等

        async函数返回值:
            async函数返回一个promise实例,默认是pending状态
            当async函数全部执行完成,async函数返回成功的promise实例,值为async函数的返回值
            当async函数内部有错误,则直接停止函数内部代码的运行,直接返回失败的promise实例,值为错误信息
            当await等待的promise实例变成失败状态,则async函数的返回值和这个失败的promise实例保持一致,并停止函数内代码的运行
            当async内部return是promise实例的时候,async函数的返回值和这个promise实例保持一致
        
        await返回值:
            就是等待promise实例成功状态的值
        */

        /* 先请求A,再请求B,再请求C */
        async function nn (){
            const u= await new Promise ((resolve,reject)=>{
                console.log('请求A');
                try {
                    // throw new Error('A错误')
                    setTimeout(() => {
                        console.log('A数据');
                        const u={
                            name:'ll'
                        }
                        resolve(u)
                    }, 1000);
                } catch (e) {
                    
                }
            })
            const bu= await new Promise ((resolve,reject)=>{
                console.log('请求B');
                try {
                    // throw new Error('B错误')
                    setTimeout(() => {
                        console.log('B数据');
                        const bu={
                            age:18
                        }
                        resolve({
                            ...u,
                            ...bu
                        })
                    }, 1000);
                } catch (e) {
                    
                }
            })
            const n= await new Promise ((resolve,reject)=>{
                console.log('请求c');
                try {
                    // throw new Error('c错误')
                    setTimeout(() => {
                        console.log('c数据');
                        const n={
                            sex:'n'
                        }
                        resolve({...bu,
                        ...n})
                    }, 1000);
                } catch (e) {
                    
                }
            })
            console.log('完整数据',n);
        }
        nn()
    </script>
</body>

</html>